from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from django.views.generic import TemplateView
from rest_framework import permissions
from drf_yasg.views import get_schema_view
from drf_yasg import openapi

# ==================== Swagger / Redoc 配置 ====================
schema_view = get_schema_view(
    openapi.Info(
        title="DRF学习项目 API",
        default_version='v1',
        description="学习DRF的接口文档",
        terms_of_service="https://www.example.com/terms/",
        contact=openapi.Contact(email="admin@example.com"),
        license=openapi.License(name="BSD License"),
    ),
    public=True,
    permission_classes=(permissions.AllowAny,),
    # authentication_classes=[]  # 🔴 禁用 drf_yasg 的登录/注销按钮

)

urlpatterns = [
    # 首页
    path('', TemplateView.as_view(template_name='index.html'), name='home'),

    path('admin/', admin.site.urls),

    # API 路由
    path('api/', include('api_examples.urls')),

    # Swagger UI 和 Redoc 文档
    path('swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
    path('redoc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),

    # DRF 认证页面 (浏览器调试用)
    path('api-auth/', include('rest_framework.urls')),

    path('accounts/', view=include('django.contrib.auth.urls')),  # 🔴 加上这一行
]

# 开发环境下提供媒体文件服务
if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
